package com.doform.android.xianggucheng.activity.money;

import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.ReqSHYSBean;
import com.doform.android.xianggucheng.bean.SHYSBean;
import com.doform.android.xianggucheng.bean.TiXianBean2;
import com.doform.android.xianggucheng.bean.YHKBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqMoney;
import com.doform.android.xianggucheng.bean.request_bean.ReqYHKBean;
import com.doform.android.xianggucheng.bean.request_bean.TiXianBean;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-08-13.
 * GitHub：https://github.com/cxydxpx
 */

public class TiXianActivity extends BaseTitleActivity {

    @BindView(R.id.tv_code)
    TextView tv_code;

    @BindView(R.id.tv_detail)
    TextView tv_detail;// "该卡本次最多可提现1.25元"

    @BindView(R.id.et_number)
    EditText et_number;

    @Override
    protected int layoutResId() {
        return R.layout.activity_tixian;
    }

    @Override
    protected void initData() {
        super.initData();
        getDataForService();
    }

    private void getDataForService() {

        Observable
                .zip(getYHK(), getSHYS(), new BiFunction<YHKBean, SHYSBean, TiXianBean>() {
                            @Override
                            public TiXianBean apply(@NonNull YHKBean yhkBean, @NonNull SHYSBean shysBean) throws Exception {
                                return new TiXianBean(yhkBean, shysBean);
                            }
                        }
                ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<TiXianBean>() {
                               @Override
                               public void onNext(@NonNull TiXianBean tiXianBean) {
                                   updateUi(tiXianBean.getYhkBean(), tiXianBean.getShysBean());
                               }
                           }
                );
    }

    private void updateUi(YHKBean yhkBean, SHYSBean shysBean) {

        tv_code.setText(yhkBean.getBankcardno());

        double v = Double.valueOf(shysBean.getSyncmaxamount()) / 100;

        tv_detail.setText("该卡本次最多可提现" + String.valueOf(v) + "元");
    }

    private Observable<SHYSBean> getSHYS() {
        return HttpManager
                .instance()
                .getSHYS(new ReqSHYSBean());
    }

    /**
     * 获取银行卡
     *
     * @return
     */
    private ObservableSource<YHKBean> getYHK() {
        return HttpManager
                .instance()
                .getYHKData(new ReqYHKBean());
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("提现");
        titleBar.setBackgroundColor(new ColorDrawable(getResources().getColor(R.color.blue2)));
    }

    @OnClick(R.id.btn_next)
    public void next() {
        beforeNext();
    }

    private void beforeNext() {
        String num = et_number.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            TsUtils.showToast("请输入提现金额");
            return;
        }
        HttpManager
                .instance()
                .getMoneyFromCard(new ReqMoney(num))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<TiXianBean2>() {
                               @Override
                               public void onNext(@NonNull TiXianBean2 s) {
                                   if (s.getSuccess().equals("true")) {
                                       TsUtils.showToast("提现成功");
                                   }
                                   et_number.setText("");
                                   getDataForService();
                               }
                           }
                );
    }
}
